package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.TopicAndKnowledgePoint;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TopicAndKnowledgePointRepository extends JpaRepository<TopicAndKnowledgePoint,Integer>, JpaSpecificationExecutor<TopicAndKnowledgePoint> {

    @Query(value = "SELECT m FROM TopicAndKnowledgePoint m WHERE m.topic.id = ?1 and  m.knowledgePoint.id =?2 ")
    TopicAndKnowledgePoint findByTopicIdAndKnowledgePointId(Integer topicId, Integer knowledgePointId);

    @Modifying
    @Query(value = "DELETE FROM TopicAndKnowledgePoint m WHERE m.topic.id = ?1")
    void deleteByTopicId(Integer topicId);

    @Modifying
    @Query(value = "DELETE FROM TopicAndKnowledgePoint m WHERE m.knowledgePoint.id = ?1")
    void deleteByKnowledgePointId(Integer knowledgePointId);

    @Query(value = "SELECT m FROM TopicAndKnowledgePoint m WHERE m.knowledgePoint.id =?1 ")
    List<TopicAndKnowledgePoint> findAllByKnowledgePointId(Integer knowledgePointId);

    @Query(value = "SELECT m FROM TopicAndKnowledgePoint m WHERE m.topic.id = ?1 ")
    List<TopicAndKnowledgePoint> findAllByTopicId(Integer topicId);
}
